package com.captain.special.二叉树;

import com.captain.leetcode.TreeNode;
import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.ArrayList;
import java.util.List;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/4/24 11:41
 */
public class 二叉树的所有路径257 {
    public List<String> binaryTreePaths(TreeNode root) {
        //深度优先搜索
        List<String> res = new ArrayList<>();
        recur(root, "", res);
        return res;
    }

    public void recur(TreeNode node, String path, List<String> res) {
        if (node != null) {
            StringBuilder builder = new StringBuilder(path);
            //前序遍历
            builder.append(node.val);
            //是否是叶子节点
            if (node.left == null && node.right == null) {
                res.add(builder.toString());
            } else {
                //如果不是叶子节点
                builder.append("->");
                //向下循环遍历
                recur(node.left, builder.toString(), res);
                recur(node.right, builder.toString(), res);
            }
        }
    }
}
